package com.example.gitclient.ui;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.gitclient.R;
import com.example.gitclient.adapter.CommitAdapter;
import com.example.gitclient.git.GitManager;
import com.example.gitclient.model.Commit;
import com.example.gitclient.model.Repository;
import com.example.gitclient.utils.RepositoryManager;

import org.eclipse.jgit.api.errors.GitAPIException;

import java.io.IOException;
import java.util.List;

/**
 * 提交历史界面，显示仓库的提交历史记录
 */
public class CommitHistoryActivity extends AppCompatActivity {
    private RecyclerView recyclerCommits;
    private TextView tvRepositoryName;
    private TextView tvCurrentBranch;

    private Repository repository;
    private GitManager gitManager;
    private RepositoryManager repositoryManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commit_history);

        // 初始化视图
        recyclerCommits = findViewById(R.id.recyclerCommits);
        tvRepositoryName = findViewById(R.id.tvRepositoryName);
        tvCurrentBranch = findViewById(R.id.tvCurrentBranch);
        
        recyclerCommits.setLayoutManager(new LinearLayoutManager(this));

        // 获取仓库路径
        String repositoryPath = getIntent().getStringExtra("repository_path");
        if (repositoryPath == null) {
            Toast.makeText(this, R.string.repository_not_found, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 初始化仓库管理器
        repositoryManager = new RepositoryManager(this);

        // 加载仓库
        loadRepository(repositoryPath);
    }

    /**
     * 加载仓库
     * @param repositoryPath 仓库路径
     */
    private void loadRepository(String repositoryPath) {
        // 查找仓库
        for (Repository repo : repositoryManager.getRepositories()) {
            if (repo.getPath().equals(repositoryPath)) {
                repository = repo;
                break;
            }
        }

        if (repository == null) {
            Toast.makeText(this, R.string.repository_not_found, Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 打开Git仓库
        try {
            gitManager = new GitManager(repository);
            updateUI();
            loadCommits();
        } catch (IOException e) {
            Toast.makeText(this, getString(R.string.failed_to_open_repository) + ": " + e.getMessage(), Toast.LENGTH_LONG).show();
            finish();
        }
    }

    /**
     * 更新UI
     */
    private void updateUI() {
        tvRepositoryName.setText(repository.getName());
        tvCurrentBranch.setText(getString(R.string.current_branch) + ": " + repository.getCurrentBranch());
    }

    /**
     * 加载提交历史
     */
    private void loadCommits() {
        try {
            List<Commit> commits = gitManager.getCommits();
            CommitAdapter commitAdapter = new CommitAdapter(commits);
            recyclerCommits.setAdapter(commitAdapter);
        } catch (GitAPIException | IOException e) {
            Toast.makeText(this, "加载提交历史失败: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (gitManager != null) {
            gitManager.close();
        }
    }


}